package com.it.security;


import com.it.security.sys.entity.User;
import com.it.security.sys.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @program: learning
 * @description: 自定义用户认证业务类
 * @author: Lefnmg
 * @create: 2018-11-19 21:40
 **/
@Service
public class JWTUserService implements UserDetailsService {
    @Autowired
    private UserService userService;
    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = new User();
        user.setUsername(username);
        user = userService.findByCondition(user);
        ArrayList<SimpleGrantedAuthority> objects = new ArrayList<>();
        objects.add(new SimpleGrantedAuthority("admin"));
        System.out.println(passwordEncoder.encode("123"));
        JWTUser jwtUser = new JWTUser(user.getId(), passwordEncoder.encode("123"), user.getUsername(), objects);
        return jwtUser;
    }
}
